# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>
# Maintainer: Fredy García <frealgagu at gmail dot com>
# Maintainer: Maxim Baz <${pkgname} at maximbaz dot com>
# Contributor: Stefan Cocora <stefan dot cocora at gmail dot com>

pkgname=skaffold
pkgver=2.9.0
pkgrel=1
pkgdesc="A command line tool that facilitates continuous development for Kubernetes applications"
arch=("loong64" "x86_64")
url="https://github.com/GoogleContainerTools/${pkgname}"
license=("Apache")
depends=("docker" "kompose")
makedepends=("go" "go-licenses" "python")
optdepends=(
  "minikube: To use Minikube"
  "kubectl: For Kubernetes support"
  "bash-completion: Tab autocompletion"
)
source=(
  "${pkgname}-${pkgver}.tar.gz::https://github.com/GoogleContainerTools/${pkgname}/archive/v${pkgver}.tar.gz"
)
sha512sums=('36dead6c308f07d0eb0f8654a0b6b52b79149700c8db885344c8eec4df9e99abfa6b8cc631f58cb80aae2e0f741d7f2bbfcffcb17de835806ab86a169130de6a')
b2sums=('44e9af36883a65a9223e18a6b82b75b872a9e9fb5706f7d9ef3ed1bcc1db9a28aab4f64987eb5d977b79c046f400286a8716b11ef8bce217e2f0bd5ba240bdc6')
_commit="ba02de8494a2cfe36249087871e9c7aa80fc535e"

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  rm -rf "${srcdir}/gopath"
  mkdir -p "${srcdir}/gopath/bin"
  mkdir -p "${srcdir}/gopath/src/github.com/GoogleContainerTools"
  ln -rTsf "${srcdir}/${pkgname}-${pkgver}" "${srcdir}/gopath/src/github.com/GoogleContainerTools/${pkgname}"

  sed -i 's/-race//g' Makefile Makefile.diag
  sed -i '/check-docs/d' hack/checks.sh
  sed -i '/check-licenses/d' hack/checks.sh
  sed -i '/check-schema-changes/d' hack/checks.sh
  sed -i '/test-generated-proto/d' hack/checks.sh
  rm --verbose pkg/skaffold/tag/git_commit_test.go
}

build() {
  cd "${srcdir}/gopath/src/github.com/GoogleContainerTools/${pkgname}"
  export GOPATH="${srcdir}/gopath"
  export PATH="${PATH}:${GOPATH}/bin"
  export VERSION="v${pkgver}"
  export COMMIT="${_commit}"
  export TREE_STATE="clean"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -mod=readonly -modcacherw -x -v -ldflags=-linkmode=external"
  make install

  # To avoid issues deleting directories next time
  go clean --modcache

  # Create completion files
  "${srcdir}/gopath/bin/${pkgname}" completion bash > "${srcdir}/${pkgname}-completion.bash"
  "${srcdir}/gopath/bin/${pkgname}" completion zsh > "${srcdir}/${pkgname}-completion.zsh"
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make test
  make -f Makefile.diag test
}

package() {
  install -Dm755 "${srcdir}/gopath/bin/${pkgname}" "${pkgdir}/usr/bin/${pkgname}"
  install -Dm644 "${srcdir}/${pkgname}-completion.bash" "${pkgdir}/usr/share/bash-completion/completions/${pkgname}"
  install -Dm644 "${srcdir}/${pkgname}-completion.zsh" "${pkgdir}/usr/share/zsh/site-functions/_skaffold"
}
